#include <iostream>
#include "InetAddr.hpp"
#include "Socket.hpp"
#include "Log.hpp"
#include <memory>
#include <poll.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <unistd.h>
using namespace SocketModule;
using namespace LogModule;
const static int SIZE = sizeof(fd_set) * 8;
const static int defaultfd = -1;
class PollServer {
public:
    PollServer(int port) : _listensock(make_unique<TcpSocket>()), _isrunning(false) {
        _listensock->BuildTcpSocketMethod(port);
        for(int i = 0; i < SIZE; i++) {
            _fds[i].fd = defaultfd;
            _fds[i].events = 0;
            _fds[i].revents = 0;
        }
        _fds[0].fd = _listensock->Fd();
        _fds[0].events = POLLIN;
    }
    void Start() {
        _isrunning = true;
        while (_isrunning) {
            PrintFd();
            int n = poll(_fds, SIZE, -1);
            if(n == 0) {
                LOG(LogLevel::INFO) << "poll timeout";
            }else if(n == -1) {
                LOG(LogLevel::ERROR) << "poll error";
            }else {
                LOG(LogLevel::DEBUG) << "有事件就绪了, n : " << n;
                Dispatcher();
            }
        }
        _isrunning = false;
    }
    void PrintFd() {
        std::cout << "_fds[]: ";
        for(int i = 0; i < SIZE; i++) {
            if(_fds[i].fd == defaultfd) {
                continue;
            }
            std::cout << _fds[i].fd << " ";
        }
        std::cout << endl;
    }
    void Stop() {
        _isrunning = false;
    }
    void Dispatcher() {
        for(int i = 0; i < SIZE; i++) {
            if(_fds[i].fd == defaultfd) continue;
            if(_fds[i].revents == POLLIN) {
                if(_fds[i].fd == _listensock->Fd()) {
                    Accepter();
                }else {
                    Recver(i);
                }
            }
        }
    }
    void Recver(int pos) {
        char buffer[1024];
        int n = recv(_fds[pos].fd, buffer, sizeof(buffer) - 1, 0);
        if(n > 0) {
            buffer[n] = '\0';
            std::cout << "client say# " << buffer << std::endl;
        }else if(n == 0) {
            LOG(LogLevel::INFO) << "client quit";
            close(_fds[pos].fd);
            _fds[pos].fd = defaultfd;
            _fds[pos].events = 0;
            _fds[pos].revents = 0;
        }else {
            LOG(LogLevel::ERROR) << "recv error";
            close(_fds[pos].fd);
            _fds[pos].fd = defaultfd;
            _fds[pos].events = 0;
            _fds[pos].revents = 0;
        }
    }
    void Accepter() {
        InetAddr client;
        int sockfd = _listensock->Accept(&client);
        if(sockfd > 0) {
            LOG(LogLevel::INFO) << "获取了一个新连接, sockfd : " << sockfd << ", client is " << client.StringAddr();
            int pos = 0;
            for(; pos < SIZE; pos++) {
                if(_fds[pos].fd == defaultfd) {
                    break;
                }
            }
            if(pos == SIZE) {
                LOG(LogLevel::WARNING) << "poll server full";
                close(sockfd);
            }else {
                _fds[pos].fd = sockfd;
                _fds[pos].events = POLLIN   ;
                _fds[pos].revents = 0;
            }
        }
    }
private:
    std::unique_ptr<Socket> _listensock;
    bool _isrunning;
    struct pollfd _fds[SIZE];
};